//  Inside Microsoft Exchange 2007 Web Services 
//  Chapter 17
//
//  Copyright (c) 2007 David Sterling, Ben Spain, Mark Taylor, Huw Upshall, Michael Mainer.  
//  All rights reserved.
//

using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.IO;
using ProxyHelpers.EWS;

namespace CreatePullSubscription
{
    class Program
    {
        static void Main(string[] args)
        {
            ExchangeServiceBinding binding = new ExchangeServiceBinding();
            binding.Url = @"https://yourServer/ews/Exchange.asmx";
            binding.Credentials = new NetworkCredential("username", "password", "domain");

            string subscriptionId;
            string watermark;

            CreatePullSubscription(
                 binding, /* ExchangeServiceBinding to use for call */
                 new NotificationEventTypeType[] { 
                    NotificationEventTypeType.CreatedEvent },
                 new BaseFolderIdType[] { 
                    new DistinguishedFolderIdType(DistinguishedFolderIdNameType.inbox) },
                 out subscriptionId,
                 out watermark);

            Console.WriteLine("Subscription Id: {0}, \r\n Watermark: {1}", subscriptionId, watermark);
        }

        /// <summary>
        /// Create a new Pull Notifications Subscription (Listing 17-4)
        /// </summary>
        /// <param name="binding">Binding to use for call</param>
        /// <param name="eventTypes">Events for which to Subscribe</param>
        /// <param name="subscriptionFolders">Folders for which to Subscribe</param>
        /// <param name="subscriptionId">Returned id for the Subscription created</param>
        /// <param name="watermark">Returned watermark for the Subscription created</param>
        ///
        public static void CreatePullSubscription(
                                     ExchangeServiceBinding binding,
                                     NotificationEventTypeType[] eventTypes,
                                     BaseFolderIdType[] subscriptionFolders,
                                     out string subscriptionId,
                                     out string watermark)
        {
            subscriptionId = string.Empty;
            watermark = string.Empty;

            // Call the overloaded constructor
            //
            PullSubscriptionRequestType pullSubscription =
                        new PullSubscriptionRequestType(
                               subscriptionFolders,
                               eventTypes,
                               string.Empty, /* New Subscription so pass in empty Watermark */
                               30 /* 30 minute timeout */);

            // Create the Subscribe request and submit the request to the server
            //
            SubscribeType subscribeRequest = new SubscribeType();
            subscribeRequest.Item = pullSubscription;
            SubscribeResponseType subscribeResponse = binding.Subscribe(subscribeRequest);

            // We have a response. Now we need to parse it to get the SubscriptionId and the 
            // Watermark
            //
            SubscribeResponseMessageType subscribeResponseMessage =
                  subscribeResponse.ResponseMessages.Items[0] as
                        SubscribeResponseMessageType;

            if (subscribeResponseMessage.ResponseCode == ResponseCodeType.NoError)
            {
                subscriptionId = subscribeResponseMessage.SubscriptionId;
                watermark = subscribeResponseMessage.Watermark;
            }
            else
            {
                // We received an error in the response
                throw new Exception(subscribeResponseMessage.MessageText);
            }
        }

    }
}
